<?php
class Container extends CI_Controller {

	var $page = 'container';
	var $action = '';


	var $message = '';

	public function index(){
		$query = $this->db->query("select c.* ,
			(select concat(firstname,' ',lastname) from users u where u.id = c.updatedby) as updatedby,
		    (select concat(firstname,' ',lastname) from users u where u.id = c.createdby) as createdby
				
			from container c where valid = 1");
			
		//echo "SET.......".$this->session->userdata('session_id');
		$data['totalRecords'] =  $query->num_rows();
			
		if($this->message != '')
		$data['successMessage'] = $this->message;

			
		$this->admin($data,$query);

	}

	public function add(){
		$this->action = 'add';
		$this->index();
	}


	public function save(){

		$name = $_REQUEST['name'];
		$description = $_REQUEST['description'];

		if($this->validateFields() == FALSE){
			$data['errorMessage'] = validation_errors();
			$this->admin($data, null);
			return;
		}
			
		$isUpdate = false;
			
			

		if(isset($_REQUEST['id']))
		$isUpdate  = true;
			
		if($isUpdate){
			$query = $this->db->query("UPDATE  container SET  name =  '$name',description =  '$description' , updatedon =  NOW() WHERE  id =".$_REQUEST['id']);

			$this->message = "Container was successfully Updated.";

			$this->createLog('CONTAINER' , 'UPDATED' ,  "Updated Container Record where id = ".$_REQUEST['id']);

		}else{
			$this->db->query("INSERT INTO  container (id ,name ,description".$this->creationFields().")
								  VALUES (NULL ,  '$name',  '$description'".$this->creationValues().")");

			$this->createLog('CONTAINER' , 'CREATED' ,  "Created new Container");

			$this->message = "New Container was successfully Created.";
		}

		//
		//$this->createLog('PAGES', )
			
		$this->index();

	}

	public function delete($id){

		$data['deleteId'] = $id;
			
		$query = $this->db->query("update container set valid = false  where id = $id limit 1" );
			
		$this->createLog('CONTAINER' , 'DELETED' ,  "Deleted Container Record where id = $id");
			
		$this->index();
			
	}

	public function edit($id){
			
		//$this->page = 'edit'.$this->page;
			
		$this->action = 'edit';
			
		$query = $this->db->query("select * from container where id = $id and valid= 1 limit 1" );
		//$row =
		$data['container'] = $query->row();

			
		$this->admin($data,$query);
			
	}

}